package com.nulldev.util.web.nTube.components.video_queue;

import java.util.concurrent.TimeUnit;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.JVM.Arguments.ParsedArguments;
import com.nulldev.util.concurrency.threadIt.v4.emapi.uft.Executable;
import com.nulldev.util.web.YouTubeExtractor.video.ExtractedVideo;
import com.nulldev.util.web.nTube.NTubeLaunchArguments.nTubeArguments;

public enum VideoQueueMode {

	/* Public Instance */

	LOW_THROUGHPUT,
	MEDIUM_THROUGHPUT,
	HIGH_THROUGHPUT,

	/* Localhost */

	UNRESTRICTED_QUEUE,

	/* Configurable */

	CUSTOM;

	public static VideoQueueMode get() {
		final ParsedArguments pa = Arguments.awaitGet();
		if (pa.has(nTubeArguments.VQM_LOW_THROUGHPUT)) {
			return LOW_THROUGHPUT;
		} else if (pa.has(nTubeArguments.VQM_MEDIUM_THROUGHPUT)) {
			return MEDIUM_THROUGHPUT;
		} else if (pa.has(nTubeArguments.VQM_HIGH_THROUGHPUT)) {
			return HIGH_THROUGHPUT;
		} else if (pa.has(nTubeArguments.VQM_CUSTOM)) {
			return CUSTOM;
		} else {
			return UNRESTRICTED_QUEUE;
		}
	}

	public static WaitableQueue<Executable<ExtractedVideo>> getCustom() {
		final ParsedArguments pa = Arguments.awaitGet();
		if (!pa.has(nTubeArguments.VQM_CUSTOM))
			throw new IllegalArgumentException("VQM_CUSTOM is not set!");
		if (!pa.has(nTubeArguments.VQM_CUSTOM_SIZE))
			throw new IllegalArgumentException("VQM_CUSTOM_SIZE is not set!");
		final int size = pa.getValue(nTubeArguments.VQM_CUSTOM_SIZE);
		if (size <= 0) {
			throw new IllegalArgumentException("Invalid VQM_CUSTOM_SIZE!");
		}
		final int delay = pa.getValue(nTubeArguments.VQM_CUSTOM_DELAY);
		if (delay <= 0) {
			throw new IllegalArgumentException("Invalid VQM_CUSTOM_DELAY!");
		}
		return new TimedQueue<Executable<ExtractedVideo>>(size, delay, TimeUnit.MILLISECONDS);
	}
}
